//
//  ReadingFooterViewController.m
//  SunXi
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "ReadingFooterViewController.h"

@interface ReadingFooterViewController () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UIButton *buttonNew;

@property(nonatomic, retain)UIButton *buttonHot;

@property(nonatomic, retain)UITableView *table;

@property(nonatomic, strong)NSMutableArray *arr;

@property(nonatomic, strong)NSMutableArray *array;

@property (nonatomic, assign)NSInteger newsLoadTimes;

@property (nonatomic, assign)NSInteger hotLoadTimes;

@property(nonatomic, retain)UIView *viewNew;

@property(nonatomic, retain)UIView *viewHot;

@end

@implementation ReadingFooterViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self create];
    [self clickNew:self.buttonNew];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - 创建页面框架
- (void)create{
    
    self.table = [[UITableView alloc] init];
    [self.view addSubview:self.table];
    self.table.rowHeight = 150;
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle = NO;
    
    self.buttonHot = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.buttonHot];
    [self.buttonHot setTitle:@"最热" forState:UIControlStateNormal];
    [self.buttonHot addTarget:self action:@selector(clickHot:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonHot.tintColor = [UIColor grayColor];
    
    
    self.buttonNew = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.buttonNew];
    [self.buttonNew setTitle:@"最新" forState:UIControlStateNormal];
    [self.buttonNew addTarget:self action:@selector(clickNew:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonNew.tintColor = BUTTONCOLOR;
    
    self.buttonNew.backgroundColor = [UIColor whiteColor];
    self.buttonHot.backgroundColor = [UIColor whiteColor];
    
    [self.buttonNew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(SV).with.offset(0);
        make.left.equalTo(SV).with.offset(0);
        make.right.equalTo(self.buttonHot.mas_left).with.offset(0);
        make.bottom.equalTo(self.table.mas_top).with. offset(0);
        make.width.equalTo(@(150));
        make.height.equalTo(@(40));
    }];
    
    [self.buttonHot mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(SV).with.offset(0);
        make.left.equalTo(self.buttonNew.mas_right).with.offset(0);
        make.right.equalTo(SV).with.offset(0);
        make.bottom.equalTo(self.table.mas_top).with. offset(0);
        make.width.equalTo(self.buttonNew);
        make.height.equalTo(self.buttonNew);
    }];
    
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.buttonNew.mas_bottom).with.offset(0);
        make.left.equalTo(SV).with.offset(0);
        make.right.equalTo(SV).with.offset(0);
        make.bottom.equalTo(SV).with. offset(0);
    }];
}


#pragma mark - 关于 tableView 的
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.arr[indexPath.row][@"firstimage"] isEqualToString:@""]) {
        static NSString *reuse1 = @"reuse1";
        ReadingDetailStandCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse1];
        if (!cell) {
            cell = [[ReadingDetailStandCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse1];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.labelTitle.text = self.arr[indexPath.row][@"title"];
        cell.labelDetail.text = self.arr[indexPath.row][@"shortcontent"];
        cell.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.1];
        return cell;
    }
    else{
        static NSString *reuse2 = @"reuse2";
        ReadingDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse2];
        if (!cell) {
            cell = [[ReadingDetailCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse2];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.labelTitle.text = self.arr[indexPath.row][@"title"];
        cell.image.text = self.arr[indexPath.row][@"shortcontent"];
        cell.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.1];
        [cell.labelDetail sd_setImageWithURL:self.arr[indexPath.row][@"firstimage"]placeholderImage:[UIImage imageNamed:@"zan"]];
        return cell;
    }
    return nil;
}




//      tableView 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [ShowMytext showHUDloadView:SV];
    
    //      点击跳到 webView 的方法
    NSString *thirdReadingPageUrl = @"http://api2.pianke.me/article/info";
    NSDictionary *para = @{@"auth":@"", @"client":@"2", @"contentid":[NSString stringWithFormat:@"%@", self.arr[indexPath.row][@"contentid"]], @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D",@"version":@"3.0.6"};
    [DyAFnetWorking POSTAfnNetworkingWithUrl:thirdReadingPageUrl WithParameters:para Block:^(id result) {
        NSLog(@"%@", result);
        NSString *str = result[@"data"][@"shareinfo"][@"url"];
        WebView *webView = [[WebView alloc]init];
        webView.tittle = result[@"data"][@"shareinfo"][@"title"];
        webView.url = str;
        [self.navigationController pushViewController:webView animated:YES];
        [ShowMytext hiddHUDloadView];
    }];
}


#pragma mark - 点击方法

//      最新的点击方法
- (void)clickNew:(UIButton *)button{
    
    self.arr = [NSMutableArray array];
    [ShowMytext showHUDloadView:SV];
    [self createNewData];
    self.table.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [self.table.mj_header beginRefreshing];
        [self createNewData];
    }];
    
    self.table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [ShowMytext showHUDloadView:SV];
        [self createNewDataNew];
        ++self.newsLoadTimes;
    }];
    
    
    if (button.tintColor == [UIColor grayColor]) {
        
        self.viewNew = [[UIView alloc] initWithFrame:CGRectMake(0, self.buttonNew.frame.size.height - 2, self.buttonNew.frame.size.width, 2)];
        self.viewNew.backgroundColor = BUTTONCOLOR;
        [self.buttonNew addSubview:self.viewNew];
        
        button.tintColor = BUTTONCOLOR;
        self.buttonHot.tintColor = [UIColor grayColor];
        [self.viewHot removeFromSuperview];
    }
}


//      最热的点击方法
- (void)clickHot:(UIButton *)button{
    
    if (button.tintColor == [UIColor grayColor]) {
        
        button.tintColor = BUTTONCOLOR;
        self.buttonNew.tintColor = [UIColor grayColor];
        [self.viewNew removeFromSuperview];
        
        self.viewHot = [[UIView alloc] initWithFrame:CGRectMake(0, self.buttonNew.frame.size.height - 2, self.buttonNew.frame.size.width, 2)];
        self.viewHot.backgroundColor = BUTTONCOLOR;
        [self.buttonHot addSubview:self.viewHot];
    }
    
    self.arr = [NSMutableArray array];
    [ShowMytext showHUDloadView:SV];
    [self createHotData];
    self.table.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [self.table.mj_header beginRefreshing];
        [self createHotData];
    }];
    self.table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [ShowMytext showHUDloadView:SV];
        [self createHotDataNew];
        self.hotLoadTimes++;
    }];
}



#pragma mark - 数据解析

//      最新加载
- (void)createNewData{
    
    NSString *str = @"http://api2.pianke.me/read/latest";
    NSDictionary *dic = @{@"auth":@"", @"client":@"2", @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D", @"limit":@"10", @"sort":@"addtime", @"start":@"0", @"version":@"3.0.6"};
    
    [DyAFnetWorking POSTAfnNetworkingWithUrl:str WithParameters:dic Block:^(id result) {
       
        self.arr = [NSMutableArray array];
        NSArray *array1 = result[@"data"][@"list"];
        
        for (NSDictionary *dic in array1) {
            [self.arr addObject:dic];
        }
        
        [self.table reloadData];
        [self.table.mj_header endRefreshing];
        [ShowMytext hiddHUDloadView];
    }];
    
}



//      最热加载
- (void)createHotData{
    
    NSString *str = @"http://api2.pianke.me/read/latest";
    NSDictionary *dic = @{@"auth":@"", @"client":@"2", @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D", @"limit":@"10", @"sort":@"hot", @"start":@"0", @"version":@"3.0.6"};
    
    [DyAFnetWorking POSTAfnNetworkingWithUrl:str WithParameters:dic Block:^(id result) {
        self.arr = [NSMutableArray array];
        NSArray *array1 = result[@"data"][@"list"];
        
        for (NSDictionary *dic in array1) {
            [self.arr addObject:dic];
        }
        [self.table reloadData];
        [self.table.mj_header endRefreshing];
        [ShowMytext hiddHUDloadView];
    }];
}


//      最新下拉刷新
- (void)createNewDataNew{
    
    NSString *str = @"http://api2.pianke.me/read/latest";
    NSDictionary *dic = @{@"auth":@"", @"client":@"2", @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D", @"limit":@"10", @"sort":@"addtime", @"start":[NSString stringWithFormat:@"%ld", (self.newsLoadTimes + 1) * 10], @"version":@"3.0.6"};
    
    [DyAFnetWorking POSTAfnNetworkingWithUrl:str WithParameters:dic Block:^(id result) {
        NSLog(@"%@", result);
        self.array = [NSMutableArray array];
        self.array = result[@"data"][@"list"];
        
        for (NSDictionary *dic in self.array) {
            [self.arr addObject:dic];
        }

        [self.table reloadData];
        [self.table.mj_footer endRefreshing];
        [ShowMytext hiddHUDloadView];
    }];
}



//      最热下拉刷新
- (void)createHotDataNew{
    NSString *str = @"http://api2.pianke.me/read/latest";
    NSDictionary *dic = @{@"auth":@"", @"client":@"2", @"deviceid":@"55887AC1-1871-4EAF-BC9F-648F8A10EE3D", @"limit":@"10", @"sort":@"hot", @"start":[NSString stringWithFormat:@"%ld", (self.hotLoadTimes + 1) * 10], @"version":@"3.0.6"};
    
    [DyAFnetWorking POSTAfnNetworkingWithUrl:str WithParameters:dic Block:^(id result) {

        self.array = [NSMutableArray array];
        self.array = result[@"data"][@"list"];
        
        for (NSDictionary *dic in self.array) {
            [self.arr addObject:dic];
        }
        
        [self.table reloadData];
        [self.table.mj_footer endRefreshing];
        [ShowMytext hiddHUDloadView];
    }];

}



//      重写返回键的方法
- (IBAction)presentLeftMenuViewController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}



@end
